package com.yinp.proapp.module.game.tanchishe;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;

import com.yinp.proapp.utils.AppUtils;

import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;

/**
 * @author :yinpeng
 * date      :2021/9/7
 * package   :com.yinp.proapp.module.game.tanchishe
 * describe  :
 */
public class GluttonousSnakeView extends View {

    public GluttonousSnakeView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public GluttonousSnakeView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private ALinkList aLinkList;
    /**
     * 蛇
     */
    private Paint snakePaint;
    private Paint snakePaint2;
    private int snakeWidth;
    /**
     * 食物
     */
    private Paint foodPaint;
    private Point foodPoint;
    /**
     * 边框
     */
    private Paint borderPaint;
    private Rect borderRect;

    private int part = 50;//能被100整除

    private void init() {
        snakePaint = new Paint();
        snakePaint2 = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.DITHER_FLAG);
        aLinkList = new ALinkList();
        foodPaint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.DITHER_FLAG);
        borderPaint = new Paint();
        borderPaint.setStrokeWidth(padding);
        borderPaint.setColor(Color.RED);
        borderPaint.setStyle(Paint.Style.STROKE);
    }

    private int width;//当前view的宽度
    private int height;//当前view的高度
    boolean isFirst = true;
    private int totalLRWidth = 0;//左右间距之和
    private int padding = 4;//边框
    private int duration = 60;//蛇的移动速度

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int screenWidth = (int) AppUtils.getWidthPixels();
        int screenHeight = (int) AppUtils.getHeightPixels();

        totalLRWidth = 100 + screenWidth % 100 - padding * 2;
        width = height = screenWidth - totalLRWidth - padding * 2;
        borderRect = new Rect(padding, padding, width - padding, height - padding);
        if (isFirst) {
            snakeWidth = width / part;
            snakePaint.setStyle(Paint.Style.STROKE);
            snakePaint.setStrokeWidth(snakeWidth);
            snakePaint.setColor(Color.RED);

            snakePaint2.setStrokeWidth(snakeWidth);
            snakePaint2.setColor(Color.TRANSPARENT);

            foodPaint.setColor(Color.BLUE);
            foodPaint.setStrokeWidth(snakeWidth);

            initSnakeAndFood();
            isFirst = false;
        }
        setMeasuredDimension(width + padding * 2, height + padding * 2);
    }

    /**
     * 设置蛇前进的方向
     */
    private int direction = 2;

    public void setDirection(int gravity) {
        if (direction + gravity == 2 || direction + gravity == 4) {
            return;
        }
        this.direction = gravity;
    }

    /**
     * 移动逻辑
     */
    private void start() {
        switch (direction) {
            case 0://左
                aLinkList.insertFirst(aLinkList.first.x - snakeWidth, aLinkList.first.y);
                break;
            case 1://上
                aLinkList.insertFirst(aLinkList.first.x, aLinkList.first.y - snakeWidth);
                break;
            case 2://右
                aLinkList.insertFirst(aLinkList.first.x + snakeWidth, aLinkList.first.y);
                break;
            case 3://下
                aLinkList.insertFirst(aLinkList.first.x, aLinkList.first.y + snakeWidth);
                break;
        }
        if (aLinkList.isFirstRepetition() || aLinkList.first.x >= width || aLinkList.first.x <= 0 || aLinkList.first.y >= height || aLinkList.first.y <= 0) {
            timer.cancel();
            duration = 100;
            isRun = false;
            isEnd = true;
            timer = null;
            timerTask = null;
        } else {
            if (foodPoint.x == aLinkList.first.x && foodPoint.y == aLinkList.first.y) {
                foodPoint = null;
                duration -= 4;
            } else {
                aLinkList.removeLast();
            }
            if (foodPoint == null) {
                randomFood();
            }
            postInvalidate();
        }
    }

    private Timer timer;
    private TimerTask timerTask;
    private boolean isRun = false;
    private boolean isEnd = false;

    /**
     * 开始移动
     */
    public void startMove() {
        if (isEnd) {
            initSnakeAndFood();
            isEnd = false;
            postInvalidate();
        }
        if (isRun) {
            return;
        }
        isRun = true;
        if (timerTask == null) {
            timerTask = new TimerTask() {
                @Override
                public void run() {
                    start();
                }
            };
        }
        if (timer == null) {
            timer = new Timer();
            timer.schedule(timerTask, 0, duration);
        }
    }

    public void reStart() {
        if (timer != null) {
            timer.cancel();
        }
        duration = 100;
        isRun = false;
        isEnd = true;
        timer = null;
        timerTask = null;
        startMove();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawPoints(aLinkList.getAll(), snakePaint);
        canvas.drawPoint(foodPoint.x, foodPoint.y, foodPaint);
        canvas.drawRect(borderRect, borderPaint);
    }

    /**
     * 食物的生成
     */
    private void randomFood() {
        Random random = new Random();
        int x = random.nextInt(part);
        int y = random.nextInt(part);
        if (x == 0) {
            x = 1;
        }
        if (y == 0) {
            y = 1;
        }
        boolean is = aLinkList.contains(x * snakeWidth, y * snakeWidth);
        if (!is) {
            foodPoint = new Point(x * snakeWidth, y * snakeWidth);
        } else {
            randomFood();
        }
    }

    /**
     * 初始化蛇的位置和食物的位置，表示重开
     */
    private void initSnakeAndFood() {
        aLinkList.clearAll();
        aLinkList.insertFirst(width / 2, width / 2);
        aLinkList.insertFirst(width / 2 + snakeWidth, width / 2);
        aLinkList.insertFirst(width / 2 + snakeWidth * 2, width / 2);
        aLinkList.insertFirst(width / 2 + snakeWidth * 3, width / 2);
        randomFood();
    }

    /**
     * 停止移动，可以再次开始
     */
    public void stopMove() {
        isRun = false;
        if (timer != null) {
            timer.cancel();
            timer = null;
        }
    }

    /**
     * 结束时
     */
    public void remove() {
        if (timer != null) {
            timer.cancel();
            timer = null;
        }
        if (timerTask != null) {
            timerTask.cancel();
            timerTask = null;
        }
    }
}
